# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=libresprite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.1
pkgrel=1
pkgdesc='Animated sprite editor & pixel art tool -- Fork of the last GPLv2 commit of Aseprite (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libresprite.github.io/'
msys2_repository_url='https://github.com/LibreSprite/LibreSprite'
license=('spdx:GPL-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-SDL2_image"
         "${MINGW_PACKAGE_PREFIX}-tinyxml2"
         "${MINGW_PACKAGE_PREFIX}-v8"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libarchive")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
source=("${_realname}"::"git+${msys2_repository_url}.git#tag=v${pkgver}"
        '0001-librescprite-1.1-resource-finder-fix.patch')
sha256sums=('0e13be335a1eeadb3116dc9bf6519ed35b56f6ed4bbe1572895e3d9b784f2158'
            '9fe1100a8723ce8e9695cfe1e3b425c91664581f35054191f4d1ba78945ee705')

prepare() {
  cd "${_realname}"

  git submodule init
  git submodule update

  patch -Np1 -i "${srcdir}"/0001-librescprite-1.1-resource-finder-fix.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
